package top.go2do.auth.business.app.controller;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.go2do.auth.business.app.module.*;
import top.go2do.auth.business.app.service.ApplicationService;
import top.go2do.common.controller.TomatoController;
import top.go2do.common.module.ApplicationPO;
import top.go2do.common.module.UserInfoBO;
import top.go2do.common.module.pageQuery.PageResult;

import java.util.List;

/**
 * @Description
 * @Author xingyj
 * @CreateTime 2025/3/18 11:48
 */
@RequestMapping("/apps")
@RestController
public class ApplicationController implements TomatoController<ApplicationPO, AppCreateForm, AppModifyForm, String, AppQueryCondition, AppQueryAllCondition, AppListVO> {

    @Autowired
    private ApplicationService applicationService;

    @Override
    public ApplicationPO create(AppCreateForm appCreateForm, UserInfoBO userInfo) {
        appCreateForm.setUserId(userInfo.getUserId());
        return applicationService.create(appCreateForm);
    }

    @Override
    public void modifyById(String id, AppModifyForm appModifyForm, UserInfoBO userInfo) {
        appModifyForm.setAppId(id);
        appModifyForm.setUserId(userInfo.getUserId());
        applicationService.modifyById(appModifyForm);
    }

    @Override
    public void removeById(String id, UserInfoBO userInfo) {
        applicationService.removeById(id);
    }

    @Override
    public ApplicationPO getById(String appId) {
        return applicationService.getById(appId);
    }

    @Override
    public List<AppListVO> listAll(AppQueryAllCondition condition) {
        List<ApplicationPO> poList = applicationService.listAll(condition);
        List<AppListVO> volist = applicationService.po2vo(poList);
        return volist;
    }

    @Override
    public PageResult<AppListVO> pageQuery(AppQueryCondition appQueryCondition) {
        PageResult<ApplicationPO> pageResult = applicationService.pageQuery(appQueryCondition);
        List<AppListVO> volist = applicationService.po2vo(pageResult.getList());
        PageResult<AppListVO> finalResult = new PageResult<>();
        BeanUtils.copyProperties(pageResult, finalResult);
        finalResult.setList(volist);
        return finalResult;
    }

}
